﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.HttpContext

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <ToolboxData("<{0}:Rating runat=server></{0}:Rating>")> _
    Public Class Rating
        Inherits WebControl
        Private _rating As Double = 0
        Private _ratingCSS As String = "afrate"
        Private _topicId As Integer = -1
        Private _userId As Integer = -1
        Protected WithEvents cb As New Callback
        Public Property Rating() As Double
            Get
                Return _rating
            End Get
            Set(ByVal value As Double)
                _rating = value
            End Set
        End Property
        Public Property RatingCSS() As String
            Get
                Return _ratingCSS
            End Get
            Set(ByVal value As String)
                _ratingCSS = value
            End Set
        End Property
        Public Property TopicId() As Integer
            Get
                Return _topicId
            End Get
            Set(ByVal value As Integer)
                _topicId = value
            End Set
        End Property
        Public Property UserId() As Integer
            Get
                Return _userId
            End Get
            Set(ByVal value As Integer)
                _userId = value
            End Set
        End Property
        Private Sub Rating_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            If Enabled Then
                cb.ID = "cb"
                Me.Controls.Add(cb)
            End If

        End Sub
        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            Dim lit As New Literal
            lit.Text = RenderRating()
            If Enabled Then
                Dim cbContent As New CallBackContent
                cbContent.Controls.Add(lit)
                cb.Content = cbContent

                cb.RenderControl(writer)
            Else
                lit.RenderControl(writer)
            End If


        End Sub

        Private Function RenderRating() As String
            If Rating > 0 Then
                Select Case Math.Round(Rating, 0)
                    Case 1
                        RatingCSS &= " onepos"
                    Case 2
                        RatingCSS &= " twopos"
                    Case 3
                        RatingCSS &= " threepos"
                    Case 4
                        RatingCSS &= " fourpos"
                    Case 5
                        RatingCSS &= " fivepos"
                End Select
            End If
            If Rating = 0 And Enabled = False Then
                Return String.Empty
            End If
            Dim sb As New StringBuilder
            sb.Append("<div class=""" & CssClass & """>")
            sb.Append("<ul class=""" & RatingCSS & """>")
            If Enabled Then
                sb.Append("<li class=""one""><a title=""[RESX:Rate:1Star]"" href=""#"" onclick=""" & cb.ClientID & ".Callback(1); return false;"">1</a></li>")
                sb.Append("<li class=""two""><a title=""[RESX:Rate:2Star]"" href=""#"" onclick=""" & cb.ClientID & ".Callback(2); return false;"">2</a></li>")
                sb.Append("<li class=""three""><a title=""[RESX:Rate:3Star]"" href=""#"" onclick=""" & cb.ClientID & ".Callback(3); return false;"">3</a></li>")
                sb.Append("<li class=""four""><a title=""[RESX:Rate:4Star]"" href=""#"" onclick=""" & cb.ClientID & ".Callback(4); return false;"">4</a></li>")
                sb.Append("<li class=""five""><a title=""[RESX:Rate:5Star]"" href=""#"" onclick=""" & cb.ClientID & ".Callback(5); return false;"">5</a></li>")
            Else
                sb.Append("<li class=""one"">1</li>")
                sb.Append("<li class=""two"">2</li>")
                sb.Append("<li class=""three"">3</li>")
                sb.Append("<li class=""four"">4</li>")
                sb.Append("<li class=""five"">5</li>")
            End If

            sb.Append("</ul>")
            sb.Append("</div>")
            Return sb.ToString


        End Function


        Private Sub cb_Callback(ByVal sender As Object, ByVal e As CallBackEventArgs) Handles cb.Callback
            Dim db As New Data.Topics
            If e.Parameters.Length > 0 Then
                Dim rate As Integer = CInt(e.Parameter)
                If rate >= 1 And rate <= 5 Then
                    Rating = db.Topics_AddRating(TopicId, UserId, rate, String.Empty, Current.Request.UserHostAddress.ToString)
                End If
            End If

            Dim cbContent As New CallBackContent
            cbContent.Controls.Add(New LiteralControl(RenderRating()))
            cb.Content = cbContent
            cb.Content.RenderControl(e.Output)
        End Sub
    End Class
End Namespace

